package com.winning.sjzc.login.service.impl;

import com.winning.sjzc.common.utils.JwtToken;
import com.winning.sjzc.login.dao.IndexMapper;
import com.winning.sjzc.login.dao.SysUserTokenMapper;
import com.winning.sjzc.login.entity.SysCzryEntity;
import com.winning.sjzc.login.entity.SysGncdEntity;
import com.winning.sjzc.login.entity.SysUserTokenEntity;
import com.winning.sjzc.login.service.LoginService;
import com.winning.sjzc.login.service.SysCzryJsService;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * description:
 *
 * @author yanxf@winning.com.cn
 * @date 2019/08/07 11:26
 */
@Service
public class LoginServiceImpl implements LoginService {

  @Autowired
  private SysUserTokenMapper sysUserTokenMapper;

  @Autowired
  private IndexMapper indexMapper;

  @Autowired
  private SysCzryJsService sysCzryJsService;

  @Override
  public String createToken(SysCzryEntity user) {
    //生成一个token
    String token = null;
    try {
      token = JwtToken.createToken(createTokenKeyMap(user));
    } catch (Exception e) {

    }
    //当前时间
    Date now = new Date();
    SysUserTokenEntity sysUserTokenEntity = new SysUserTokenEntity();
    sysUserTokenEntity.setToken(token);
    sysUserTokenEntity.setUserId(user.getZclsh());
    sysUserTokenEntity.setUpdateTime(now);
    sysUserTokenEntity.setExpireTime(JwtToken.getExpireTime("b"));
    sysUserTokenMapper.insert(sysUserTokenEntity);
    return token;
  }

  @Override
  public List<SysGncdEntity> queryNavigationMenu(List ryjsCollect) {
    return indexMapper.queryNavigationMenu(ryjsCollect);
  }

  @Override
  public Map<String, Object> querySubMenu(String mcode, List<String>  ryjsCollect) {
    Map<String, Object> resultMap = new HashMap<>();

    List<SysGncdEntity> secondMenu = indexMapper.querySubMenu(mcode, ryjsCollect);

    List<SysGncdEntity> secondList = new ArrayList<>();
    if (secondMenu.size() > 0) {

      for (SysGncdEntity sge : secondMenu) {
        List list = new ArrayList<>();
        SysGncdEntity sysge = new SysGncdEntity();
        sysge = sge;
        List<SysGncdEntity> thirdMenu = indexMapper.querySubMenu(sge.getMcode(), ryjsCollect);
        if (thirdMenu.size() > 0) {
          sysge.setSubFlag("1");
          sysge.setThirdlist(thirdMenu);
        } else {
          sysge.setSubFlag("0");
        }
        list.add(sysge);
        secondList.addAll(list);
      }
    }
    resultMap.put("secondMenu", secondList);
    return resultMap;
  }

  private Map<String,String> createTokenKeyMap(SysCzryEntity user){
    Map<String,String> keyMap = new HashMap<String,String>(6);
    keyMap.put("zclsh",user.getZclsh());
    keyMap.put("orgcode",user.getJgbm());
    keyMap.put("jgbm",user.getJgbm());
    keyMap.put("zygh",user.getRygh());
    keyMap.put("zyxm",user.getRyxm());
    keyMap.put("ksbm", StringUtils.isEmpty(user.getKsbm())?"":user.getKsbm());
    return keyMap;
  }
}
